package com.erp.stock.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.stock.dao.CooperativesRawMaterialInventoryDao;
import com.erp.stock.entity.CooperativesRawMaterialInventory;
import com.erp.stock.service.CooperativesRawMaterialInventoryService;
import org.reflections.vfs.Vfs;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CooperativesRawMaterialInventoryServiceImpl implements CooperativesRawMaterialInventoryService {
    @Autowired
    CooperativesRawMaterialInventoryDao dao;

    @Override
    public Boolean update(CooperativesRawMaterialInventory entity) {
        CooperativesRawMaterialInventory cooperativesRawMaterialInventory = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity,cooperativesRawMaterialInventory);
        return dao.saveAndFlush(cooperativesRawMaterialInventory) != null;
    }

    @Override
    public Boolean add(CooperativesRawMaterialInventory cooperativesRawMaterialInventory) {
        return dao.save(cooperativesRawMaterialInventory)!= null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<CooperativesRawMaterialInventory> listPage(int page, int size) {
        return dao.findAll(PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id"))).getContent();
    }

    @Override
    public List<CooperativesRawMaterialInventory> listPageLike(int page, int size, String column) {
        return dao.findAllByCooperativesNameLike(StringUtil.addLike(column),PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id")));
    }
}
